//
//  LYJUnit.m
//  Unit
//
//  Created by mac on 17/2/10.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "LYJUnit.h"
#import "LYJActionSheet.h"
#import <CoreLocation/CoreLocation.h>
#define IS_IOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)

#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

#define IS_IOS9 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0)

@implementation LYJUnit

+ (void)_registerUserNotificationSettings
{
    if (IS_IOS7)
    {
        [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:1];
    }
    
    if (IS_IOS8)
    {
        UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
        
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        
        [[UIApplication sharedApplication]  registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication]  registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge];
    }
}

+ (BOOL)_hasOpenLocation
{
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (kCLAuthorizationStatusDenied == status || kCLAuthorizationStatusRestricted == status)
    {
        return NO;
    }
    return YES;
}

+ (void)_showAlert:(NSString *)message title:(NSString *)title
{
    static UIAlertView *alert = nil;
    if ([title length] == 0)
    {
        title = @"好的";
    }
    if (alert.superview != nil)
    {
        return;
    }
    
    alert = [[UIAlertView alloc] initWithTitle:nil message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:title, nil];
    [alert show];
}

+ (UIButton *)_buttonWithTtile:(NSString *)title
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateHighlighted];
    [button setTitle:title forState:UIControlStateSelected];
    return button;
}


+ (UILabel *)_labelWithFrame:(CGRect)frame
                   textColor:(UIColor *)textColor
                        text:(NSString *)text
               textAlignment:(NSTextAlignment)textAlignment
                        font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc]initWithFrame:frame];
    label.text = text;
    label.textAlignment = textAlignment;
    label.font = font;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = textColor;
    label.lineBreakMode = NSLineBreakByClipping;
    label.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    return label;
}

+ (void)_cornerRadius:(CGFloat)cornerRadius
          borderWidth:(CGFloat)borderWidth
          borderColor:(UIColor *)borderColor
        masksToBounds:(BOOL)masksToBounds
                 view:(UIView *)view
{
    view.layer.cornerRadius = cornerRadius;
    if (borderColor)
    {
        view.layer.borderColor = borderColor.CGColor;
    }
    view.layer.masksToBounds = masksToBounds;
    view.layer.borderWidth = borderWidth;
}


+ (LYJActionSheet *)_actionSheetWithTitle:(NSString *)title
                        cancelButtonTitle:(NSString *)cancelButtonTitle
                        otherButtonTitles:(NSArray *)otherButtonTitles
                               clickBlock:(void (^)(NSInteger))clickBlock
                             dismissBlock:(void (^)(NSInteger))dismissBlock
{
    LYJActionSheet *actionSheet = [LYJActionSheet _actionSheetWithTitle:title cancelButtonTitle:cancelButtonTitle destructiveButtonTitle:nil otherButtonTitles:otherButtonTitles];
    actionSheet.clickBlock = ^(NSInteger index){
        if (clickBlock)
        {
            clickBlock(index);
        }
    };
    actionSheet.dismissBlock = ^(NSInteger index){
        if (dismissBlock)
        {
            dismissBlock(index);
        }
    };
    return actionSheet;
}
@end
